package com.apache.uct.common;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import com.apache.api.entity.BaseEntity;
import com.apache.tools.StrUtil;
import com.apache.uct.common.entity.Act;
import com.apache.uct.common.entity.User;

/**
 * 登录用户
 * @version 1.2.3
 */
@SuppressWarnings("serial")
public class LoginUser extends BaseEntity {

	//	/** 系统名称：非业务属性，暂未使用*/
	//	private String sysEname;
	private final String setSplit = "|";
	private final String getSplit = "\\|";

	private User user;

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		if (ToolsUtil.isEmpty(user))
			return;
		this.user = user;
		this.email = user.getEmail();
		this.userType = user.getUserType();
		this.mobile = user.getMobile();
		this.phone = user.getPhone();
		this.userId = user.getUserId();
		this.userEname = user.getUserEname();
		this.userCname = user.getUserCname();
		this.userStatus = user.getUserStatus();
		this.delStatus = user.getDelStatus();
		this.sysName = user.getSysEname();
		this.orgId = user.getOrgId();
		this.deptId = user.getDeptId();
		this.userPass = user.getUserPass();
	}

	/** 
	 * 用户拥有的权限集合map，
	 * key为权限的全路径，格式：/utr/user/user_add
	 * value为对应权限实例
	 * 判断权限方法：actMap.containsKey("/utr/user/user_add") 返回true表示拥有该权限
	 **/
	private Map<String, Act> actMap = new HashMap<String, Act>();

	/**数据权限：机构 "10001,1002,1003"*/
	private String dataOrgIds = "";

	/**数据权限：部门  "10001,1002,1003"*/
	private String dataDeptIds = "";

	private String ipAddress;

	private String sysFlag = "0";//是否为系统用户 0机构用户；1系统用户；2机构管理员

	/**
	 * description:  是否为系统用户
	 */
	public String getSysFlag() {
		return sysFlag;
	}

	public void setSysFlag(String sysFlag) {
		this.sysFlag = sysFlag;
	}

	/**
	 * 用户拥有的部门数据权限，格式： "10001,1002,1003"
	 * @return
	 */
	public String getDataDeptIds() {
		return dataDeptIds;
	}

	public void setDataDeptIds(String dataDeptIds) {
		this.dataDeptIds = dataDeptIds;
	}

	public String getDataOrgIds() {
		return dataOrgIds;
	}

	public void setDataOrgIds(String dataOrgIds) {
		this.dataOrgIds = dataOrgIds;
	}

	//用户拥有的权限集合
	public Map<String, Act> getActMap() {
		return actMap;
	}

	public void setActMap(Map<String, Act> actMap) {
		this.actMap = actMap;
	}

	/**
	 * 用户拥有的部门数据权限，格式： "'10001','1002','1003'
	 * @return
	 */
	public String getDataDeptIdAryStr() {

		return StrUtil.strArrayToStr(dataDeptIds.split(","), ",", "'");
	}

	/**
	 * 用户拥有的机构数据权限，格式： "'10001','1002','1003'
	 * @return
	 */
	public String getDataOrgIdAryStr() {
		return StrUtil.strArrayToStr(dataOrgIds.split(","), ",", "'");
	}

	/**
	 * 将用户基本信息转化为字符串，便于存放在cookie中
	 * @return String
	 */
	public String toString_base() {
		StringBuffer sb = new StringBuffer();
		sb.append(user.getUserId());
		sb.append(setSplit).append(user.getUserEname());
		sb.append(setSplit).append(user.getUserPass());
		sb.append(setSplit).append(user.getUserCname());
		sb.append(setSplit).append(user.getOrgId());
		sb.append(setSplit).append(user.getOrgEname());
		sb.append(setSplit).append(user.getUserType());
		return sb.toString();
	}

	/**
	 * 将用户拥有的钥匙转化为字符串形式
	 * @return
	 */
	public String toString_keys() {
		StringBuffer sb2 = null;
		if (actMap != null && actMap.size() > 0) {
			Iterator it = actMap.keySet().iterator();
			while (it.hasNext()) {
				//钥匙的fullEname做的key
				String key = (String) (it.next());
				Act act = (Act) actMap.get(key);
				if (sb2 != null) {
					sb2.append(setSplit).append(act.getFullEname());
				} else {
					sb2 = new StringBuffer(act.getFullEname());
				}
			}
		}
		return sb2 == null ? "" : sb2.toString();
	}

	public String toString() {
		if (this == null)
			return null;
		StringBuffer sb = new StringBuffer();
		sb.append(",").append("userId=" + user.getUserId());
		sb.append(",").append("userEname=" + user.getUserEname());
		sb.append(",").append("userPass=" + user.getUserPass());
		sb.append(",").append("userCname=" + user.getUserCname());
		sb.append(",").append("orgId=" + user.getOrgId());
		sb.append(",").append("orgEname=" + user.getOrgEname());
		//sb.append(",").append("isSupperUser="+this.isSupperUser());
		sb.append(",").append("userType=" + user.getUserType());
		sb.append(",").append("keys=" + this.toString_keys());
		return sb.toString();
	}

	public String getIpAddress() {
		return ipAddress;
	}

	public void setIpAddress(String ipAddress) {
		this.ipAddress = ipAddress;
	}

	private String dataDomain;//机构所属域
	private String userPass;
	private String passwordWeak;
	private String userType;
	private String userId;
	private String userEname;
	private String userCname;
	private Integer userStatus;
	private String orgId;
	private String orgEname;
	private String orgCname;
	private String deptId;
	private String deptEname;
	private String deptCname;
	private String vcode;
	private String actJsonStr;
	private String email;
	private String phone;
	private String mobile;
	private String delStatus;
	private String sysName;
	private String customerId;
	private String customerNo;
	private String customerName;

	@Deprecated
	public String getDataDomain() {
		return dataDomain;
	}

	public void setDataDomain(String dataDomain) {
		this.dataDomain = dataDomain;
	}

	@Deprecated
	public String getUserPass() {
		return userPass;
	}

	public void setUserPass(String userPass) {
		this.userPass = userPass;
	}

	@Deprecated
	public String getPasswordWeak() {
		return passwordWeak;
	}

	public void setPasswordWeak(String passwordWeak) {
		this.passwordWeak = passwordWeak;
	}

	@Deprecated
	public String getUserType() {
		return userType;
	}

	public void setUserType(String userType) {
		this.userType = userType;
	}

	@Deprecated
	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	@Deprecated
	public String getUserEname() {
		return userEname;
	}

	public void setUserEname(String userEname) {
		this.userEname = userEname;
	}

	@Deprecated
	public String getUserCname() {
		return userCname;
	}

	public void setUserCname(String userCname) {
		this.userCname = userCname;
	}

	@Deprecated
	public Integer getUserStatus() {
		return userStatus;
	}

	public void setUserStatus(Integer userStatus) {
		this.userStatus = userStatus;
	}

	@Deprecated
	public String getOrgId() {
		return orgId;
	}

	public void setOrgId(String orgId) {
		this.orgId = orgId;
	}

	@Deprecated
	public String getOrgEname() {
		return orgEname;
	}

	public void setOrgEname(String orgEname) {
		this.orgEname = orgEname;
	}

	public String getOrgCname() {
		return orgCname;
	}

	public void setOrgCname(String orgCname) {
		this.orgCname = orgCname;
	}

	@Deprecated
	public String getDeptId() {
		return deptId;
	}

	public void setDeptId(String deptId) {
		this.deptId = deptId;
	}

	public String getDeptEname() {
		return deptEname;
	}

	public void setDeptEname(String deptEname) {
		this.deptEname = deptEname;
	}

	public String getDeptCname() {
		return deptCname;
	}

	public void setDeptCname(String deptCname) {
		this.deptCname = deptCname;
	}

	@Deprecated
	public String getVcode() {
		return vcode;
	}

	public void setVcode(String vcode) {
		this.vcode = vcode;
	}

	public String getActJsonStr() {
		return actJsonStr;
	}

	public void setActJsonStr(String actJsonStr) {
		this.actJsonStr = actJsonStr;
	}

	@Deprecated
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	@Deprecated
	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	@Deprecated
	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	@Deprecated
	public String getDelStatus() {
		return delStatus;
	}

	public void setDelStatus(String delStatus) {
		this.delStatus = delStatus;
	}

	@Deprecated
	public String getSysName() {
		return sysName;
	}

	public void setSysName(String sysName) {
		this.sysName = sysName;
	}

	public String getSetSplit() {
		return setSplit;
	}

	public String getGetSplit() {
		return getSplit;
	}

	public String getCustomerId() {
		return customerId;
	}

	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}

	public String getCustomerNo() {
		return customerNo;
	}

	public void setCustomerNo(String customerNo) {
		this.customerNo = customerNo;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

}
